package com.sxfq.loansupermarket.backsystem.modular.arrival.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.arrival.dao.ReturnVisitDao;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.IReturnVisitService;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ReturnVisitServiceImpl implements IReturnVisitService {
    @Autowired
    private ReturnVisitDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<ReturnVisitInfo> selectPageListByQueryGetMap(Page<ReturnVisitInfo> page, SearchReturnVisitInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getOutpatientName())){
            params.put("outpatientName", searchInfo.getOutpatientName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getTcName())) {
            params.put("tcName", searchInfo.getTcName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollowStatus())) {
            params.put("followStatus", searchInfo.getFollowStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        params.put("tableName", ShiroKit.getUser().getCustomer_info());
        return dao.getQueryPage(page, params);
    }

    public List<ReturnVisitInfo> selectPageListByQuery(Page<ReturnVisitInfo> page, SearchReturnVisitInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartFilingTime())) {
            params.put("startFilingTime", searchInfo.getStartFilingTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndFilingTime())) {
            params.put("endFilingTime", searchInfo.getEndFilingTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartRevisitTime())) {
            params.put("startRevisitTime", searchInfo.getStartRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollower())) {
            params.put("follower", searchInfo.getFollower());
        }
        if (StringUtil.isNotEmpty(searchInfo.getArchiving())) {
            params.put("archiving", searchInfo.getArchiving());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStatus())) {
            params.put("status", searchInfo.getStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDepartment())) {
            params.put("department", " a.follower in (select t1.account from user t1 where  t1.roleid='"+searchInfo.getDepartment()+"') ");
        }
        if (StringUtil.isNotEmpty(searchInfo.getOutpatientName()) && !"请选择".equals(searchInfo.getOutpatientName())) {
            String outpatientName = "";
            String outpatientName2[] = searchInfo.getOutpatientName().split(",");
            if (outpatientName2.length == 1) {
                outpatientName = outpatientName2[0];
            } else {
                for (int i = 0; i < outpatientName2.length; i++) {
                    if (i == (outpatientName2.length - 1)) {
                        outpatientName += "'" + outpatientName2[i];
                    } else if (i == 0) {
                        outpatientName += outpatientName2[i] + "'" + ",";
                    } else {
                        outpatientName += "'" + outpatientName2[i] + "'" + ",";
                    }
                }
            }
            params.put("outpatientName", " a.outpatient_name in ('" + outpatientName + "')");
        }
        if (StringUtil.isNotEmpty(searchInfo.getArea())) {
            String area = "";
            String area2[] = searchInfo.getArea().split(",");
            if (area2.length == 1) {
                area = area2[0];
            } else {
                for (int i = 0; i < area2.length; i++) {
                    if (i == (area2.length - 1)) {
                        area += "'" + area2[i];
                    } else if (i == 0) {
                        area += area2[i] + "'" + ",";
                    } else {
                        area += "'" + area2[i] + "'" + ",";
                    }
                }
            }
            params.put("area", " a.outpatient_name  in (select a.simplename from dept a where a.area_abbreviation in ('" + area + "'))");
        }
        if (StringUtil.isNotEmpty(searchInfo.getAccessDisease())) {
            params.put("accessDisease", searchInfo.getAccessDisease());
        }
        if (!"请选择".equals(searchInfo.getOneChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
                params.put("oneChannel", searchInfo.getOneChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getTwoChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
                params.put("twoChannel", searchInfo.getTwoChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getThrChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
                params.put("thrChannel", searchInfo.getThrChannel());
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getAppointmentPayService())) {
            params.put("appointmentPayService", searchInfo.getAppointmentPayService());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStatuss())) {
            params.put("statuss", searchInfo.getStatuss());
        }

        String listHuifangTime="left(substring_index(a.huifang,',<b>',LENGTH(huifang) - LENGTH(huifang)-1),16)";
        params.put("tableName", ShiroKit.getUser().getCustomer_info());
        params.put("listHuifangTime", listHuifangTime);
        return dao.getQueryPage1(page, params);
    }

    @Override
    public List<ReturnVisitInfo> reservationsExportt(SearchReturnVisitInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartFilingTime())) {
            params.put("startFilingTime", searchInfo.getStartFilingTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndFilingTime())) {
            params.put("endFilingTime", searchInfo.getEndFilingTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartRevisitTime())) {
            params.put("startRevisitTime", searchInfo.getStartRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollower())) {
            params.put("follower", searchInfo.getFollower());
        }
        if (StringUtil.isNotEmpty(searchInfo.getArchiving())) {
            params.put("archiving", searchInfo.getArchiving());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStatus())) {
            params.put("status", searchInfo.getStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDepartment())) {
            params.put("department", " a.follower in (select t1.account from user t1 where  t1.roleid='"+searchInfo.getDepartment()+"') ");
        }
        if (StringUtil.isNotEmpty(searchInfo.getOutpatientName()) && !"请选择".equals(searchInfo.getOutpatientName())) {
            String outpatientName = "";
            String outpatientName2[] = searchInfo.getOutpatientName().split(",");
            if (outpatientName2.length == 1) {
                outpatientName = outpatientName2[0];
            } else {
                for (int i = 0; i < outpatientName2.length; i++) {
                    if (i == (outpatientName2.length - 1)) {
                        outpatientName += "'" + outpatientName2[i];
                    } else if (i == 0) {
                        outpatientName += outpatientName2[i] + "'" + ",";
                    } else {
                        outpatientName += "'" + outpatientName2[i] + "'" + ",";
                    }
                }
            }
            params.put("outpatientName", " a.outpatient_name in ('" + outpatientName + "')");
        }
        if (StringUtil.isNotEmpty(searchInfo.getArea())) {
            String area = "";
            String area2[] = searchInfo.getArea().split(",");
            if (area2.length == 1) {
                area = area2[0];
            } else {
                for (int i = 0; i < area2.length; i++) {
                    if (i == (area2.length - 1)) {
                        area += "'" + area2[i];
                    } else if (i == 0) {
                        area += area2[i] + "'" + ",";
                    } else {
                        area += "'" + area2[i] + "'" + ",";
                    }
                }
            }
            params.put("area", " a.outpatient_name  in (select a.simplename from dept a where a.area_abbreviation in ('" + area + "'))");
        }

        if (StringUtil.isNotEmpty(searchInfo.getAccessDisease())) {
            params.put("accessDisease", searchInfo.getAccessDisease());
        }
        if (!"请选择".equals(searchInfo.getOneChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
                params.put("oneChannel", searchInfo.getOneChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getTwoChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
                params.put("twoChannel", searchInfo.getTwoChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getThrChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
                params.put("thrChannel", searchInfo.getThrChannel());
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getAppointmentPayService())) {
            params.put("appointmentPayService", searchInfo.getAppointmentPayService());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStatuss())) {
            params.put("statuss", searchInfo.getStatuss());
        }

        String listHuifangTime="left(substring_index(a.huifang,',<b>',LENGTH(huifang) - LENGTH(huifang)-1),16)";
        params.put("tableName", ShiroKit.getUser().getCustomer_info());
        params.put("listHuifangTime", listHuifangTime);
        return dao.getQueryPageExport(params);
    }

    public CustomerInfo selectOneById(String customerId) {
        String sql = "select * from " + ShiroKit.getUser().getCustomer_info() + " where id= '" + customerId + "';";
        CustomerInfo customerInfo = sqlMapper.selectOne(sql, CustomerInfo.class);
        return customerInfo;
    }
}